﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace _IMES.DBUtils
{
    public class DBConvert
    {
        /// <summary>
        /// 将DataTable中的数据拷贝到指定类型的list对象中
        /// </summary>
        /// <typeparam name="R">新对象类型</typeparam>
        /// <param name="dt">数据表对象</param>
        /// <returns></returns>
        public static List<R> DataTableToList<R>(DataTable dt)
            where R : new()
        {
            //获取传泛型的具体属性
            Type resultType = typeof(R);
            List<R> listR = new List<R>(dt.Rows.Count);
            R r;
            DataColumn dataCol;
            object value;
            //获取类的具体属性及属性的数据类型
            PropertyInfo[] propertyInfos = resultType.GetProperties();
            int[] propertyValueIndex = new int[propertyInfos.Length];
            int i, j;
            DataRow dataRow;

            for (j = 0; j < propertyInfos.Length; j++)
            {
                dataCol = dt.Columns[propertyInfos[j].Name];
                propertyValueIndex[j] = dataCol == null ? -1 : dataCol.Ordinal;
            }

            for (i = 0; i < dt.Rows.Count; i++)
            {
                dataRow = dt.Rows[i];
                r = new R();
                for (j = 0; j < propertyInfos.Length; j++)
                {
                    if (propertyValueIndex[j] != -1)
                    {
                        value = dataRow[propertyValueIndex[j]];
                        propertyInfos[j].SetValue(r, value is DBNull ? null : value);
                    }
                }
                listR.Add(r);
            }

            return listR;
        }


        /// <summary>
        /// 将DataRow转换成实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T DataRowToModel<T>(DataRow dr)
        {
            try
            {
                Type temp = typeof(T);
                T obj = Activator.CreateInstance<T>();
                foreach (DataColumn column in dr.Table.Columns)
                {
                    foreach (PropertyInfo pro in temp.GetProperties())
                    {
                        if (pro.Name.ToLower() == column.ColumnName.ToLower())
                        {
                            if (dr[column.ColumnName] == DBNull.Value)
                            {
                                pro.SetValue(obj, " ", null);
                                break;
                            }
                            else
                            {
                                pro.SetValue(obj, dr[column.ColumnName], null);
                                break;
                            }
                        }
                    }
                }
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
